<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员姓名" prop="consumerName">
        <el-input
          v-model="queryParams.consumerName"
          placeholder="请输入会员姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="consumerGenerate">
        <el-input
          v-model="queryParams.consumerGenerate"
          placeholder="请输入性别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="consumerPhone">
        <el-input
          v-model="queryParams.consumerPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bath:consumer:add']"
        >新增</el-button>
      </el-col>
     <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['bath:consumer:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['bath:consumer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="46px"/>
      <el-table-column label="会员姓名" align="center" prop="consumerName" />
      <el-table-column label="性别" align="center" prop="consumerGenerate" width="46"/>
      <el-table-column label="手机号" align="center" prop="consumerPhone" width="110"/>
      <el-table-column label="介绍经理" align="center" prop="mId" />
      <el-table-column label="会员QQ" align="center" prop="consumerQQ" width="100"/>
      <el-table-column label="会员微信" align="center" prop="consumerVx" width="100"/>
      <el-table-column label="累计次数" align="center" prop="consumerTimes" width="100" />
      <el-table-column label="会员卡等级" align="center" prop="consumerCard" :formatter="consumerCardFormat"/>
      <el-table-column label="上次消费日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次消费金额" align="center" prop="price" />
       <el-table-column label="备注" align="center" prop="note" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
             @click="updateTimes(scope.row)"
            v-hasPermi="['bath:consumer:edit']"
          >增加次数</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bath:consumer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bath:consumer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员姓名" prop="consumerName">
          <el-input v-model="form.consumerName" placeholder="请输入会员姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="consumerGenerate">
          <el-input v-model="form.consumerGenerate" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="手机号" prop="consumerPhone">
          <el-input v-model="form.consumerPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="介绍经理" prop="mId">
          <el-input v-model="form.mId" placeholder="请输入介绍经理" />
        </el-form-item>
        <el-form-item label="会员QQ" prop="consumerQQ">
          <el-input v-model="form.consumerQQ" placeholder="请输入会员QQ" />
        </el-form-item>
        <el-form-item label="会员微信" prop="consumerVx">
          <el-input v-model="form.consumerVx" placeholder="请输入会员微信" />
        </el-form-item>
        <el-form-item label="累计次数" prop="consumerTimes">
          <el-input v-model="form.consumerTimes" disabled/>
        </el-form-item>
        <el-form-item label="会员卡等级">
          <el-radio-group v-model="form.consumerCard">
            <el-radio
              v-for="dict in consumerCardOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上次消费日期" prop="date">
          <el-date-picker clearable size="small"
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上次消费日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上次消费金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入上次消费金额" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input  type="textarea" :rows="2" v-model="form.note" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加会员本次消费金额 -->
    <el-dialog :title="title" :visible.sync="openPrice" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
       
        <el-form-item label="本次消费金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入本次消费金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPrice">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumer, updateCount,getConsumer, delConsumer, addConsumer, updateConsumer, exportConsumer } from "@/api/bath/consumer";

export default {
  name: "Consumer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      consumerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openPrice:false,
      // 会员卡等级字典
      consumerCardOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consumerName: null,
        consumerGenerate: null,
        consumerPhone: null,
      },
      // 表单参数
      form: {},
      
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_level").then(response => {
      this.consumerCardOptions = response.data;
    });
  },
  methods: {
   
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listConsumer(this.queryParams).then(response => {
        this.consumerList = response.rows;
        this.total = response.total;
        this.loading = false;
        
      });
    },
    // 会员卡等级字典翻译
    consumerCardFormat(row, column) {
      return this.selectDictLabel(this.consumerCardOptions, row.consumerCard);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openPrice=false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        consumerName: null,
        consumerGenerate: null,
        consumerPhone: null,
        mId: null,
        consumerQQ: null,
        consumerVx: null,
        consumerTimes: "1",
        consumerCard: "0",
        date: null,
        price: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息";
      
      
    },
     // 增加次数
    updateTimes(row){
      const id = row.id || this.ids
      getConsumer(id).then(response => {
        this.form.id = row.id;
        this.form.consumerCard=response.data.consumerCard;
        this.form.consumerTimes = response.data.consumerTimes;
        this.form.consumerTimes=this.form.consumerTimes+1;
        this.form.price=response.data.price
        this.form.consumerName=response.data.consumerName
        this.openPrice=true;
         this.title = "本次消费金额";
        if(this.form.consumerTimes>0 && this.form.consumerTimes<=9){
          this.form.consumerCard=1;
        }else if(this.form.consumerTimes>=10 && this.form.consumerTimes<=19){
          this.form.consumerCard=10;
        }else if(this.form.consumerTimes>=20 && this.form.consumerTimes<=29){
          this.form.consumerCard=20;
        }
         
     });
    },
    
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConsumer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员信息";
      });
    },
    //提交本次消费金额
    submitPrice(){
  this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
              updateCount(this.form).then(response => {  
              this.msgSuccess("次数添加成功");
              this.getList();
              this.openPrice=false;
                });
          }
        }
  });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConsumer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConsumer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportConsumer(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
